package applicationFunction.fileSystem;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;

public class FileText {

    public static Long startTime = LocalDateTime.of(2020, 7, 8, 21, 0).toInstant(ZoneOffset.of("+8")).toEpochMilli();

    public static Long endTime = LocalDateTime.of(2020, 7, 8, 22, 10).toInstant(ZoneOffset.of("+8")).toEpochMilli();

    public static String orginPath = "D:\\maven\\repository";

    public static String outPutPath = "E:\\tmp\\swagger\\lib";

    public static void main(String[] args) {

        File file = new File(orginPath);        //获取其file对象
        func(file);
    }

    /**
     * 将file文件夹及其子文件夹下面近几个小时的文件复制到新的目录下面
     * @param file
     */
    private static void func(File file) {
        File[] fs = file.listFiles();
        for (File f : fs) {
            if (f.isDirectory())    //若是目录，则递归打印该目录下的文件
                func(f);
            if (f.isFile())        //若是文件，直接打印
            {
                try {
                    BasicFileAttributes bAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
                    if (bAttributes.lastModifiedTime().toMillis() > startTime && bAttributes.lastModifiedTime().toMillis() < endTime) {
                        System.out.println(f.getAbsolutePath() + " " + bAttributes.lastModifiedTime());

                        File destFile = new File(outPutPath + f.getAbsolutePath().replace(orginPath, ""));
                        if (!destFile.getParentFile().exists()) {
                            destFile.getParentFile().mkdirs();
                        }
                        try {
                            Files.copy(f.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
